import util from './util.js';
import { login } from './../api/user.js';

export default function authLogin(){
  return new Promise((reslove, reject) => {
    util.autoLogin().then(userInfo => {
      var params = {
		    sessionKey: userInfo.cache_key || '',
        code: userInfo.code,
        encryptedData: userInfo.encryptedData,
        rawData: userInfo.rawData,
        signature: userInfo.signature,
        iv: userInfo.iv
      }
      login(params).then(res => {
        getApp().globalData.token = res.token;
        getApp().globalData.userInfo = res.userInfo;
        getApp().globalData.isLogin = true;
        if (res.sessionKey) wx.setStorage({ key: 'cache_key', data: res.sessionKey });
        reslove();
      }).catch(err=>{
        reject();
      });
    }).catch(err=>{
      //reject();
      console.log(err)
    });
  })
}